package com.zw.yzk.kabu.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.zw.yzk.kabu.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressLint("HandlerLeak")
public class SlideBar extends View {

    private String[] totalSpell = {"A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};

    private List<String> spells = new ArrayList<>();
    private final int CLOSE_SHOW_HEADER_VIEW = 1;
    public final int UPDATE_SLIDER_BAR = 2;

    private int height;
    private int singleHeight;
    private Paint mPaint;
    private PopupWindow mShowText;
    private View mShowView;
    private TextView mHeader;
    private OnSlideBarClickListener mSlideListener;
    private float density;

    public SlideBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.density = context.getResources().getDisplayMetrics().density;

        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#333333"));
        mPaint.setTextSize(density * 14);

        mShowView = LayoutInflater.from(context).inflate(
                R.layout.dialog_silde_show_text, null);
        mHeader = (TextView) mShowView.findViewById(R.id.contact_show_text_view);
        mShowText = new PopupWindow(mShowView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        if (isInEditMode()) {
            setTotalSpell();
        }
    }

    public SlideBar(Context context) {
        this(context, null, 0);
    }

    public interface OnSlideBarClickListener {
        int onSlideBarClicked(String s);
    }

    public void setOnSliderBarClickListener(OnSlideBarClickListener listener) {
        mSlideListener = listener;
    }

    public void setSpells(List<String> list) {
        if (list == null) {
            return;
        }
        this.spells.clear();
        this.spells.addAll(list);
        invalidate();
    }

    public void setTotalSpell() {
        this.spells.clear();
        this.spells.addAll(Arrays.asList(totalSpell));
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        height = getHeight();
        singleHeight = height / 27;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();

        int xPos;
        int yPos;
        for (int i = 0; i < spells.size(); i++) {
            xPos = (int) (width * (0.5f) - mPaint
                    .measureText(spells.get(i)) * (0.5f));
            yPos = singleHeight * i + (height - singleHeight * (spells.size() - 1)) / 2;
            canvas.drawText(spells.get(i), xPos, yPos, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        int item = (y - (height - singleHeight * (spells.size())) / 2) / singleHeight;
        item = item >= spells.size() ? spells.size() - 1 : item;
        item = item > 0 ? item : 0;
        if (mSlideListener != null) {
            int show = mSlideListener.onSlideBarClicked(spells.get(item));
            if (show < 0) {
                return true;
            }
            showPopup(spells.get(item));
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                showPopup(spells.get(item));
                break;
            case MotionEvent.ACTION_MOVE:
                showPopup(spells.get(item));
                break;
            case MotionEvent.ACTION_UP:
                dismissPopup();
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }

    private void showPopup(String header) {
        mHeader.setText(header);
        mShowText.showAtLocation(this, Gravity.CENTER, 0, 0);
    }

    private void dismissPopup() {
        Message msg = new Message();
        msg.what = CLOSE_SHOW_HEADER_VIEW;
        mSliderBarHandler.sendMessageDelayed(msg, 500);

    }

    private void clearPopupWindow() {
        if (mShowText == null) {
            return;
        }
        if (mShowText.isShowing()) {
            mShowText.dismiss();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        clearPopupWindow();
    }

    public Handler mSliderBarHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLOSE_SHOW_HEADER_VIEW:
                    if (mShowText.isShowing()) {
                        mShowText.dismiss();
                    }
                    break;
                case UPDATE_SLIDER_BAR:
                    invalidate();
                default:
                    break;
            }
        }

    };
}
